# -*- coding:utf-8 -*-
"""
@Author : 审心
@Time : 2023/3/21 15:51
配置相关包
"""

import yaml
import os


class YamlConfig:
    def __init__(self, filename):
        # 获取当前脚本所在的目录
        current_dir = os.path.dirname(os.path.abspath(__file__))
        # 获取项目根目录
        root_dir = os.path.dirname(current_dir)
        self.filename = root_dir + "\\config\\" + filename
        # 读取配置文件
        with open(self.filename, "r", encoding='utf-8') as f:
            self.config = yaml.safe_load(f)

    def get(self, key):
        keys = key.split(".")
        value = self.config
        for k in keys:
            value = value[k]
        return value

    def set(self, key, value):
        keys = key.split(".")
        obj = self.config
        for k in keys[:-1]:
            obj = obj[k]
        obj[keys[-1]] = value

    def save(self):
        # 写入配置文件
        with open(self.filename, "w") as f:
            yaml.safe_dump(self.config, f, default_flow_style=False)


data = YamlConfig("_local.yaml")
# data = YamlConfig("_prod_.yaml")
